<script>
    export default {
        props: {
            action:{
                type:String,
                default:'edit'
            },
            value:{

            }
        },
        inheritAttrs:false,
        data () {
            return {
                schema:{},
                temp: "",
                template: function () {
                    return null
                }
            }
        },
        render(h){
            return h('div', [this.template()]);
        },
        created() {
            this.schema = this.$attrs['schema'];
//            this.$debug.log(this.$attrs)//注意这里
            this.Ready();
        },
        computed:{
            errorTip:function (){
                if(typeof this.$attrs['error']=='object'){
                    return this.$attrs['error'].toString();
                }else{
                    return this.$attrs['error'];
                }
            }
        },
        methods: {
            Ready() {
                this.temp=this.chooseComponent(this.schema);
                this.init();
            },
            chooseComponent(schema){
                var html = '';
                var bind = this.getBind(schema);
                var type='input';

                switch (schema.type){
                    //可以对其他的组建 做特殊化处理
                    default :
                        type = schema.type;
                        if( !Vue.component(`lar-field-${type}`) ){
                            type = 'input';
                        }
                        html = `<lar-field-${type}
                            ${bind}
                            :action="action"
                            :value="value"
                            v-on = "$listeners"
                            >
                            </lar-field-${type}>`;
                        break;
                }
                return html;
            },
            //解析需要额外绑定的参数
            getBind(schema){
                var bindData ='';
                for(var key in schema){
                    let ukey = key.substring(0,1).toUpperCase()+key.substring(1);
                    bindData += ` :field${ukey}="schema['${key}']"`;
                }
                for(var key in this.$attrs){
                    bindData += `  :${key}="$attrs['${key}']"`;
                }
                return bindData;
            },
            //渲染模板
            init() {
                let result = Vue.compile(this.temp);
                this.template = result.render;
            },
        },
    }
</script>